package labels

import (
	"GinTemplate/internal/models"
	"GinTemplate/internal/services/labels"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

// Search
//
//	@Summary	get Label
//	@Tags		labels
//	@Produce	application/json
//	@Router		/api/v1/labels [get]
//	@Success 200 {object} models.GenericData[models.GenericListData[models.Label]]
//	@Failure 400 {object} models.GenericResponse
//	@Failure 500 {object} models.GenericResponse
func Search(ctx *gin.Context) {
	all, total, err := labels.GetAll()
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"message": "Update success",
		})
	}
	ctx.JSON(http.StatusOK, gin.H{
		"message": "ok",
		"data": gin.H{
			"items": all,
			"total": total,
		},
	})
}

// Get
//
//	@Summary	get Label by ID
//	@Tags		labels
//	@Produce	application/json
//	@Param		id	path	int	true	"ID"
//	@Router		/api/v1/labels/{id} [get]
//	@Success 200 {object} models.GenericData[models.Label]
//	@Failure 400 {object} models.GenericResponse
//	@Failure 500 {object} models.GenericResponse
func Get(ctx *gin.Context) {
	idStr := ctx.Param("id")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{
			"message": "id is not int",
		})
		return
	}
	label, err := labels.GetLabelById(id)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"message": "Internal error",
		})
		return
	}

	ctx.JSON(http.StatusOK, models.GenericData[models.Label]{
		GenericResponse: models.GenericResponse{
			Message: "Ok",
			Code:    http.StatusOK,
		},
		Data: label,
	})

}

// Delete
//
//	@Summary	del Label by ID
//	@Tags		labels
//	@Produce	application/json
//	@Param		id	path	int	true	"ID"
//	@Router		/api/v1/labels/{id} [delete]
//	@Success 200 {object} models.GenericResponse
//	@Failure 400 {object} models.GenericResponse
//	@Failure 500 {object} models.GenericResponse
func Delete(ctx *gin.Context) {
	idStr := ctx.Param("id")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{
			"message": "id is not int",
		})
	}
	err = labels.Delete(id)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"message": "Internal error",
		})
	} else {
		ctx.JSON(http.StatusOK, gin.H{
			"message": "Delete success",
		})
	}
}

// Create
//
//	@Summary	创建Label
//	@Tags		labels
//	@Produce	application/json
//	@Accept		application/json
//	@Param		label	body	models.CreateLabel	true	"Label"
//	@Router		/api/v1/labels [post]
//	@Success 200 {object} models.GenericResponse
//	@Failure 400 {object} models.GenericResponse
//	@Failure 500 {object} models.GenericResponse
func Create(ctx *gin.Context) {
	var label models.Label
	err := ctx.ShouldBindJSON(&label)
	if err != nil {
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{
			"message": "Parameter error",
		})
	}
	err = labels.Add(&label)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"message": "Internal error",
		})
	} else {
		ctx.JSON(http.StatusOK, gin.H{
			"message": "Create success",
		})
	}
}

// Update
//
//	@Summary	更新Label
//	@Tags		labels
//	@Produce	application/json
//	@Accept		application/json
//	@Param		id	path	int	true	"ID"
//	@Param		label	body	models.UpdateLabel	true	"Label"
//	@Router		/api/v1/labels/{id} [put]
//	@Success 200 {object} models.GenericResponse
//	@Failure 400 {object} models.GenericResponse
//	@Failure 500 {object} models.GenericResponse
func Update(ctx *gin.Context) {
	idStr := ctx.Param("id")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{
			"message": "id is not int",
		})
	}
	var labelUpdate models.UpdateLabel
	err = ctx.BindJSON(&labelUpdate)
	if err != nil {
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{
			"message": "Parameter error",
		})
	}

	err = labels.Update(id, &labelUpdate)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"message": "Internal error",
		})
	} else {
		ctx.JSON(http.StatusOK, gin.H{
			"message": "Update success",
		})
	}

}
